package wangm;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import wangm.bean.ExaminationPaper;
import wangm.bean.ExaminationTopic;

import java.text.SimpleDateFormat;
import java.util.*;

/**
 * describe
 *
 * @author zk
 * @date 2018-01-22 16:17
 */
public class Test {
    public static void main(String[] args) throws Exception {
        ExaminationPaper paperBean = new ExaminationPaper();
        paperBean.setId("001");

        List<ExaminationTopic> topicBeans = new ArrayList<>();

        ExaminationTopic topicBean = new ExaminationTopic();
        topicBean.setId("010");
        topicBean.setOptionedIds(new String[]{"030"});
        topicBeans.add(topicBean);

        ExaminationTopic topicBean1 = new ExaminationTopic();
        topicBean1.setId("010");
        topicBean1.setOptionedIds(new String[]{"030"});
        topicBeans.add(topicBean1);

        ExaminationTopic topicBean2 = new ExaminationTopic();
        topicBean2.setId("010");
        topicBean2.setOptionedIds(new String[]{"030"});
        topicBeans.add(topicBean2);

        paperBean.setTopics(topicBeans);
        System.out.println(JSON.toJSONString(paperBean));
    }

    @org.junit.Test
    public void jsonTest(){
        Map<String,Object> result = new HashMap<>();

        Map<String,String> map = new HashMap();
        map.put("1","011");
        map.put("2","021");
        map.put("like[1]","031");
        map.put("like[2]","032");
        map.put("like[3]","033");
        map.put("no[1]","041");
        map.put("no[2]","042");
        map.put("no[3]","043");
        //遍历该map
        for(String key : map.keySet()){
            String value = map.get(key);

            //如果包含[和],就认为是数组
            if(key.contains("[") && key.contains("]")){
                key = key.substring(0,key.lastIndexOf("["));
                //如果结果中无该key
                if(!result.containsKey(key)){
                    List valueList= new ArrayList();
                    valueList.add(value);
                    result.put(key,valueList);
                } else {
                    //如果有该key
                    List valueList = (List) result.get(key);
                    valueList.add(value);
                    result.put(key,valueList);
                }
            } else {
                //如果不是数组
                result.put(key,value);
            }
        }
        //以json字符串的形式输出
        System.out.println(JSONObject.toJSONString(result));
    }

    @org.junit.Test
    public void testHttp(){
        System.out.println(System.currentTimeMillis());
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(1520985600000L);
        calendar.set(Calendar.HOUR_OF_DAY,0);
        calendar.set(Calendar.MINUTE,0);
        calendar.set(Calendar.SECOND,0);
        System.out.println(calendar.getTimeInMillis());
    }

    @org.junit.Test
    public void testTimestamp(){
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        calendar.set(Calendar.DAY_OF_MONTH,calendar.get(Calendar.DAY_OF_MONTH) -2);
        System.out.println(calendar.getTimeInMillis());
        System.out.println(new SimpleDateFormat("yyyy-MM-dd").format(new Date(calendar.getTimeInMillis())));
    }
}
